package com.base.caselib;

import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.caselib.bean.TodoBean;
import com.base.caselib.databinding.ActivityTodoBinding;
import com.base.caselib.presenter.TodoPresenter;
import com.base.caselib.view.TodoView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.utils.RxBus2;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by weikailiang on 2019/10/31.
 */
@Route(path = "/todo/to")
public class TodoActivity extends CBaseBarMvpActivity<TodoPresenter> implements TodoView{

    private ActivityTodoBinding mBinding;
    private int mDealType = 1;
    private int page = 1;
    private BaseAdapter<TodoBean.RowsBean> mAdapter;
    private List<TodoBean.RowsBean> mData = new ArrayList<>();
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_todo;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityTodoBinding) mRootBinding;
        mPresenter = new TodoPresenter(this,this);
        mPresenter.start();
    }

    @Override
    protected void start() {
        setBackNavigation();
        setBarTitle("待办事宜");
        mBinding.tab.setSelectedTabIndicatorHeight(0);
        final TabLayout.Tab tab1 = mBinding.tab.newTab();
        tab1.setText("未处理");
        mBinding.tab.addTab(tab1, true);
        TabLayout.Tab tab2 = mBinding.tab.newTab();
        tab2.setText("已处理");
        mBinding.tab.addTab(tab2, false);
        mBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0) {
                    mDealType = 1;
                } else {
                    mDealType = 0;
                }
                reloadData(true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mBinding.rl.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.rl.setHeaderHeight(60);
        mBinding.rl.setRefreshFooter(new ClassicsFooter(mContext));

        mAdapter = new BaseAdapter<TodoBean.RowsBean>(mData, mContext) {

            @Override
            public void convert(BaseHolder holder,TodoBean.RowsBean bean, boolean isHeader, boolean isBottom) {
                TextView title = holder.getView(R.id.title);
                TextView time = holder.getView(R.id.time);
                time.setText(bean.getCreateDate());
                title.setText(bean.getContent());
            }
        };
        mAdapter.putNormalType(R.layout.item_todo);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                TodoBean.RowsBean todoEntity = mData.get(dataPosition);
                if (mDealType==1){
                    if (todoEntity.getTodoSource()==null){
                        showMes("找不到该案件类型");
                        return;
                    }
                    //未处理
                    if (todoEntity.getTodoSource().equals("02300003")){
                        ARouter.getInstance().build("/case/advance")
                                .withString("cformId", todoEntity.getFormId())
                                .withString("id", todoEntity.getBillId())
                                .withBoolean("istod", true)
                                .withString("busId", todoEntity.getBusinessId())
                                .withString("applyUserId", todoEntity.getApplyUserId())
                                .withString("proId", todoEntity.getProId())
                                .withString("todoId",todoEntity.getId())
                                .withString("content", todoEntity.getContent())
                                .navigation();
                    }else {
                        ARouter.getInstance().build("/case/casedetail")
                                .withString("cformId",todoEntity.getFormId())
                                .withString("id",todoEntity.getBillId())
                                .withBoolean("istod",true)
                                .withString("todoId",todoEntity.getId())
                                .withString("busId",todoEntity.getBusinessId())
                                .withString("applyUserId",todoEntity.getApplyUserId())
                                .withString("proId",todoEntity.getProId())
                                .withString("content",todoEntity.getContent())
                                .navigation();
                    }
                }else if (mDealType==0){
                    //已处理
                    if (todoEntity.getTodoSource().equals("02300003")){
                        ARouter.getInstance().build("/case/advance")
                                .withString("cformId",todoEntity.getFormId())
                                .withString("id",todoEntity.getBillId())
                                .withBoolean("istod",false)
                                .withString("todoId",todoEntity.getId())
                                .withString("busId",todoEntity.getBusinessId())
                                .withString("proId",todoEntity.getProId())
                                .withString("content",todoEntity.getContent())
                                .withString("applyUserId",todoEntity.getApplyUserId())
                                .navigation();
                    }else {
                        ARouter.getInstance().build("/case/casedetail")
                                .withString("cformId",todoEntity.getFormId())
                                .withString("id",todoEntity.getBillId())
                                .withBoolean("istod",false)
                                .withString("todoId",todoEntity.getId())
                                .withString("busId",todoEntity.getBusinessId())
                                .withString("proId",todoEntity.getProId())
                                .withString("content",todoEntity.getContent())
                                .withString("applyUserId",todoEntity.getApplyUserId())
                                .navigation();
                    }

                }


            }
        });
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rv.setAdapter(mAdapter);

        Disposable disposable = RxBus2.getInstance().doSubscribe(TodoBean.class, new Consumer<TodoBean>() {
            @Override
            public void accept(TodoBean randomRefreshEvent) throws Exception {
                mBinding.rl.autoRefresh();
            }
        });
        RxBus2.getInstance().addSubscription(this, disposable);


        mBinding.rl.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                reloadData(false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                reloadData(true);
            }
        });
        mBinding.rl.autoRefresh();
    }
    public void reloadData(boolean isRefresh){
        if (isRefresh){
            page =1;
        }else {
            page ++;
        }
        if (mDealType == 1){
            mPresenter.getTodoList(page,mDealType,isRefresh,0);
        }else {
            mPresenter.getTodoList(page,mDealType,isRefresh,1);
        }

    }

    @Override
    public void returnRefreshList(List<TodoBean.RowsBean> list) {
        mBinding.rl.finishRefresh();
        mBinding.empty.setVisibility(View.GONE);
        mBinding.rl.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(list);
        mAdapter.notifyDataSetChanged();

    }

    @Override
    public void returnRefreshNoData() {
        mBinding.rl.finishRefresh();
        mBinding.rl.setLoadmoreFinished(false);
        mBinding.empty.setVisibility(View.VISIBLE);
        mData.clear();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnLoadMoreList(List<TodoBean.RowsBean> list) {
        mBinding.rl.finishLoadmore();
        mBinding.rl.setLoadmoreFinished(false);
        mBinding.empty.setVisibility(View.GONE);
        mData.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnLoadMoreNoData() {
        mBinding.rl.finishLoadmore();
        mBinding.empty.setVisibility(View.GONE);
        mBinding.rl.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        if (page ==1){
            mBinding.rl.finishRefresh();
        }else {
            mBinding.rl.finishLoadmore();
        }
        showError();
    }
}
